package com.xhtt.common.utils;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.xhtt.modules.sys.entity.SysDeptEntity;
import com.xhtt.modules.sys.service.SysUserService;
import com.xhtt.modules.task.model.car.CarTaskUserStatus;
import org.springframework.context.ApplicationContext;

import java.util.List;
import java.util.stream.Collectors;

public class DeptUtil {
    private static SysUserService sysUserService = SpringContextUtils.getBean(SysUserService.class);

    public static void getChildren(SysDeptEntity p, List<SysDeptEntity> list, boolean needUser) {
        List<SysDeptEntity> children = list.stream().filter(roleEntity -> roleEntity.getParentId().intValue() == p.getDeptId().intValue()).collect(Collectors.toList());
        if(needUser)
        {
            List<CarTaskUserStatus> us = sysUserService.getUsersByDept(p);
            p.setCarTaskUserStatusList(us);
        }
        if (CollectionUtils.isNotEmpty(children)) {
            p.setChildren(children);
            for (SysDeptEntity child : children) {
                getChildren(child, list, needUser);
            }
        }
    }
}
